<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>修改密码</span>
        </div>
      </template>
      <el-form
        ref="passwordFormRef"
        :model="passwordForm"
        :rules="passwordRules"
        label-width="120px"
        class="password-form"
      >
        <el-form-item label="当前密码" prop="oldPassword">
          <el-input
            v-model="passwordForm.oldPassword"
            type="password"
            placeholder="请输入当前密码"
            show-password
          />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="passwordForm.newPassword"
            type="password"
            placeholder="请输入新密码"
            show-password
          />
        </el-form-item>
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input
            v-model="passwordForm.confirmPassword"
            type="password"
            placeholder="请再次输入新密码"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">修改密码</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/store/user'
import { useThemeStore } from '@/store/theme'
import { changePassword } from '@/api/user'

const router = useRouter()
const userStore = useUserStore()
const passwordFormRef = ref(null)

const passwordForm = ref({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const validateConfirmPassword = (rule, value, callback) => {
  if (value !== passwordForm.value.newPassword) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

const passwordRules = {
  oldPassword: [
    { required: true, message: '请输入当前密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ]
}

const submitForm = async () => {
  if (!passwordFormRef.value) return
  
  await passwordFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        await changePassword({
          old_password: passwordForm.value.oldPassword,
          new_password: passwordForm.value.newPassword,
          confirm_password: passwordForm.value.confirmPassword
        })
        
        ElMessage.success('密码修改成功，请重新登录')
        
        // 退出登录
        await userStore.logout()
        router.push('/login')
      } catch (error) {
        console.error('修改密码失败:', error)
        ElMessage.error(error.response?.data?.message || '修改密码失败')
      }
    }
  })
}

const resetForm = () => {
  if (passwordFormRef.value) {
    passwordFormRef.value.resetFields()
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 0;
  display: flex;
  justify-content: center;
  
  .box-card {
    width: 100%;
    max-width: 600px;
    border: 1px solid var(--border-color);
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.06);
    border-radius: 8px;
    
    :deep(.el-card__header) {
      background-color: var(--card-bg-color);
      border-bottom: 1px solid var(--border-color);
      border-radius: 8px 8px 0 0;
      
      .card-header {
        text-align: center;
        
        span {
          color: var(--text-color);
          font-weight: 600;
          font-size: 18px;
        }
      }
    }
    
    :deep(.el-card__body) {
      background-color: var(--card-bg-color);
      padding: 40px;
    }
  }
}

.password-form {
  max-width: 500px;
  margin: 0 auto;
  
  .el-form-item {
    margin-bottom: 28px;
    
    .el-form-item__label {
      color: var(--text-color);
      font-weight: 500;
      font-size: 14px;
    }
    
    .el-input {
      .el-input__wrapper {
        border-radius: 6px;
        transition: all 0.3s ease;
        padding: 12px 15px;
        
        &:hover {
          border-color: #c0c4cc;
        }
        
        &.is-focus {
          border-color: #409eff;
          box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
        }
        
        .el-input__inner {
          font-size: 14px;
        }
      }
    }
    
    &:last-child {
      margin-bottom: 0;
      margin-top: 40px;
      text-align: center;
      
      .el-button {
        border-radius: 6px;
        font-weight: 500;
        padding: 12px 30px;
        font-size: 14px;
        transition: all 0.3s ease;
        margin: 0 10px;
        
        &:hover {
          transform: translateY(-1px);
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        }
        
        &.el-button--primary {
          background: linear-gradient(135deg, #409eff 0%, #66b1ff 100%);
          border: none;
          
          &:hover {
            background: linear-gradient(135deg, #66b1ff 0%, #409eff 100%);
          }
        }
      }
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .app-container {
    padding: 20px;
    
    .box-card {
      :deep(.el-card__body) {
        padding: 30px 20px;
      }
    }
  }
  
  .password-form {
    .el-form-item {
      &:last-child {
        .el-button {
          width: 100%;
          margin: 5px 0;
        }
      }
    }
  }
}
</style>
